#include "playlistbasemodel.h"
#include <QDateTime>
#include <QDebug>
#include <time.h>

PlayListBaseModel::PlayListBaseModel() :
    QObject(0)
{
    userDataDic = new EZTally::EZTallyDictionary<std::string,EZTally::EZTallyObject*>;
    curIndex = 0;
    playQueueType = QUEUE_TYPE_RepeatList;
    songs = new EZTally::EZTallyArray<SongModel*>();

}

PlayListBaseModel::~PlayListBaseModel()
{
    if(songs)
        songs->Release();
    if(userDataDic)
        userDataDic->Release();
}

PlayListBaseModel::QUEUE_TYPE PlayListBaseModel::getPlayQueueType() const
{
    return playQueueType;
}

void PlayListBaseModel::setPlayQueueType(const QUEUE_TYPE &value)
{
    playQueueType = value;
    emit sigPlayQueueTypeChange();
}
int PlayListBaseModel::getCurIndex() const
{
    return curIndex;
}

void PlayListBaseModel::setCurIndex(int value)
{
    curIndex = value;
}
EZTally::EZTallyArray<SongModel*> *PlayListBaseModel::getSongs() const
{
    return songs;
}
int PlayListBaseModel::count()
{
    return songs->Count();
}

int PlayListBaseModel::indexOfSong(SongModel *song)
{
    return songs->IndexOfObject(song);
}

SongModel* PlayListBaseModel::songAtIndex(int index)
{
    if(index<0 || index>=songs->Count()){
        return 0;
    }
    return songs->ObjectAtIndex(index);
}

SongModel* PlayListBaseModel::nextSong(int *outIndex)
{
    SongModel *song = 0;
    int newindex = 0;
    switch(playQueueType){
        case QUEUE_TYPE_RepeatList:
        {
             newindex = (curIndex+1)%count();
        }
            break;
        case QUEUE_TYPE_RandPlay:
        {
            newindex = QDateTime::currentMSecsSinceEpoch()%count();
        }
            break;
        case QUEUE_TYPE_RepeatOne:
        {
            newindex = curIndex;
        }
        break;
    }
    song = songAtIndex(newindex);
    if(outIndex!=0){
        *outIndex = newindex;
    }
    qDebug() << "next index:" << newindex;
    return song;
}

SongModel* PlayListBaseModel::previoursSong(int *outIndex)
{
    SongModel *song = 0;
    int newindex = 0;
    switch(playQueueType){
        case QUEUE_TYPE_RepeatList:
        {
             newindex = (curIndex-1+count())%count();
        }
            break;
        case QUEUE_TYPE_RandPlay:
        {
            qsrand(time(NULL));
            newindex = qrand()%count();
        }
            break;
        case QUEUE_TYPE_RepeatOne:
        {
            newindex = curIndex;
        }
        break;
    }
    song = songAtIndex(newindex);
    if(outIndex!=0){
        *outIndex = newindex;
    }
    return song;
}

SongModel* PlayListBaseModel::curSong()
{
    return songAtIndex(curIndex);
}

int PlayListBaseModel::wantGetMoreSong()
{
    return -1;
}
EZTally::EZTallyObject* PlayListBaseModel::getUserData(const std::string &key)
{
    return userDataDic->ObjectForKey(key);
}

void PlayListBaseModel::setUserData(const std::string &key,EZTally::EZTallyObject *objData)
{
    userDataDic->SetObjectForKey(key,objData);
}
QVariant PlayListBaseModel::getUserData(const QString &key)
{
    if(_userDataMap.constFind(key)==_userDataMap.end()){
        return QVariant(QVariant::Invalid);
    }
    return _userDataMap[key];
}

void PlayListBaseModel::setUserData(const QString &key,const QVariant &value)
{
    _userDataMap.insert(key,value);
}

void PlayListBaseModel::addDelegate(PlayListBaseModelDelegate *value)
{
    _listDelegate.append(value);
}

void PlayListBaseModel::removeDelegate(PlayListBaseModelDelegate *value)
{
    int index = _listDelegate.indexOf(value);
    if(index>=0 && index<_listDelegate.size()){
        _listDelegate.removeAt(index);
    }
}
QString PlayListBaseModel::name() const
{
    return _name;
}

void PlayListBaseModel::setName(const QString &name)
{
    _name = name;
}




